package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.MerchantApplication;
import com.draven.mall.entity.RiderApplication;
import com.draven.mall.service.ApplicationService;
import com.masiyi.github.template.GithubTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * 入驻申请控制器
 */
@RestController
@RequestMapping("/api")
public class ApplicationController {

    @Autowired
    private ApplicationService applicationService;
    
    @Value("${file.upload.path}")
    private String uploadPath;

    @Autowired
    private GithubTemplate githubTemplate;

    /**
     * 商家入驻申请
     */
    @PostMapping("/merchant/apply")
    public ApiResponse applyMerchant(@RequestBody MerchantApplication application) {
        boolean success = applicationService.submitMerchantApplication(application);
        if (success) {
            return ApiResponse.success("商家入驻申请提交成功");
        } else {
            return ApiResponse.error("商家入驻申请提交失败");
        }
    }

    /**
     * 骑手入驻申请
     */
    @PostMapping("/rider/apply")
    public ApiResponse applyRider(@RequestBody RiderApplication application) {
        boolean success = applicationService.submitRiderApplication(application);
        if (success) {
            return ApiResponse.success("骑手入驻申请提交成功");
        } else {
            return ApiResponse.error("骑手入驻申请提交失败");
        }
    }

    /**
     * 查询商家申请状态
     */
    @GetMapping("/merchant/application/status")
    public ApiResponse getMerchantApplicationStatus(@RequestParam String phone) {
        Map<String, Object> result = applicationService.getMerchantApplicationStatus(phone);
        return ApiResponse.success(result);
    }

    /**
     * 查询骑手申请状态
     */
    @GetMapping("/rider/application/status")
    public ApiResponse getRiderApplicationStatus(@RequestParam String phone) {
        Map<String, Object> result = applicationService.getRiderApplicationStatus(phone);
        return ApiResponse.success(result);
    }

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public ApiResponse uploadFile(@RequestParam("file") MultipartFile file) {
        // 保存文件
        UUID uuid = UUID.randomUUID();
        String fileUrl = null;
        try {
            fileUrl = githubTemplate.putObject(file.getInputStream(), "提交", file.getOriginalFilename()+ uuid.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //   String fileUrl = applicationService.uploadFile(file);
        if (fileUrl != null) {
            return ApiResponse.success(fileUrl);
        } else {
            return ApiResponse.error("文件上传失败");
        }
    }
    
    /**
     * 文件访问 - 直接通过控制器提供文件
     */
    @GetMapping("/files/**")
    public ResponseEntity<Resource> getFile(HttpServletRequest request) {
        String requestPath = request.getRequestURI();
        // 从请求路径中提取文件路径
        String filePath = requestPath.substring("/api/files".length());
        // 构建完整的文件路径
        File file = new File(uploadPath + filePath);
        
        if (file.exists() && file.isFile()) {
            Resource resource = new FileSystemResource(file);
            return ResponseEntity.ok()
                    .contentType(MediaType.IMAGE_JPEG) // 这里假设是JPEG图片，实际使用时应根据文件类型设置
                    .body(resource);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
} 